{% extends 'dashboard/layout.html' %}
{% load currency_filters %}
{% load sorting_tags %}
{% load i18n %}

{% block body_class %}orders{% endblock %}
{% block title %}
{% trans "Order management" %} | {{ block.super }}
{% endblock %}

{% block breadcrumbs %}
<ul class="breadcrumb">
    <li>
        <a href="{% url dashboard:index %}">{% trans "Dashboard" %}</a>
        <span class="divider">/</span>
    </li>
    <li class="active">{% trans "Order management" %}</li>
</ul>
{% endblock %}

{% block header %}
<div class="page-header">
    <h1>{% trans "Order management" %}</h1>
</div>
{% endblock header %}

{% block dashboard_content %}
<div class="table-header">
    <h3><i class="icon-search icon-large"></i>{% trans "Search" %}</h3>
</div>
<div class="well">
    <form action="." method="get" class="form-inline">
        {% for field in form %}
            {% if "order" in field.id_for_label %}
                {% if field.is_hidden %}
                    {{ field }}
                {% else %}
                <span class="control-group {% if field.errors %}error{% endif %}">
                    {{ field.label_tag }}
                    {{ field }}
                    {% for error in field.errors %}
                        <ul class="help-block">
                            <li>{{ error }}</li>
                        </ul>
                    {% endfor %}
                </span>
                {% endif %}
            {% endif %}
        {% endfor %}
        <input type="submit" value="{% trans "Search" %}" class="btn btn-primary" />
        <a data-toggle="modal" href="#SearchModal">{% trans "Advanced Search" %}</a>
    </form>

    <div class="modal hide fade" id="SearchModal">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal">×</button>
            <h3>{% trans "Advanced Search" %}</h3>
        </div>
        <form action="." method="get" class="form-horizontal">
            <div class="modal-body">
                {% csrf_token %}
                {% include "partials/form_fields.html" with form=form %}
            </div>
            <div class="modal-footer">
                <a href="#" class="btn" data-dismiss="modal">{% trans "Close" %}</a>
                <button type="submit" class="btn btn-primary">{% trans "Search" %}</button>
            </div>
        </form>
    </div>
</div>

{% if orders|length %}
<form action="." method="post" class="order_table">
    {% csrf_token %}
    {% include "dashboard/orders/partials/bulk_edit_form.html" with status=active_status %}

<table class="table table-striped table-bordered table-hover">

    <caption>
        <h3 class="pull-left"><i class="icon-shopping-cart icon-large"></i>{{ queryset_description }}</h3>
        <div class="pull-right">
            <div class="form-inline">
                <label>{% trans "Download selected orders as a CSV" %}</label>
                <input type="hidden" name="action" value="download_selected_orders" />
                <button type="submit" class="btn btn-primary">{% trans "Download" %}</button>
            </div>
        </div>
    </caption>

    <thead>
        <tr>
            <th>
                {% trans "Select all" %}
            </th>
            <th>{% anchor 'number' _("Order number") %}</th>
            <th>{% trans "Total inc tax" %}</th>
            <th>{% trans "Date of purchase" %}</th>
            <th>{% trans "Number of items" %}</th>
            <th>{% trans "Status" %}</th>
            <th>{% trans "Customer" %}</th>
            <th>{% trans "Shipping address" %}</th>
            <th>{% trans "Billing address" %}</th>
            <th></th>
        </tr>
    </thead>
    {% for order in orders %}
    <tr>
        <td><input type="checkbox" name="selected_order" class="selected_order" value="{{ order.id }}"/>
        <td>{{ order.number }}</td>
        <td>{{ order.total_incl_tax|currency }}</td>
        <td>{{ order.date_placed }}</td>
        <td>{{ order.num_items }}</td>
        <td>{{ order.status|default:"-" }}</td>
        <td>{{ order.email }}</td>
        <td>{{ order.shipping_address|default:"-" }}</td>
        <td>{{ order.billing_address|default:"-" }}</td>
        <td>
            <a class="btn btn-info" href="{% url dashboard:order-detail order.number %}">{% trans "View" %}</a>
        </td>
    </tr>
    {% endfor %}
</table>

{% include "dashboard/orders/partials/bulk_edit_form.html" with status=active_status %}
{% include "partials/pagination.html" %}

</form>

{% else %}
<table class="table table-striped table-bordered">
    <caption><i class="icon-shopping-cart icon-large"></i>{{ queryset_description }}</caption>
    <tr><td>{% trans "No orders found." %}</td></tr>
</table>
{% endif %}

{% endblock dashboard_content %}

{% block onbodyload %}
oscar.dashboard.orders.initTable();
oscar.dashboard.search.init();
{% endblock onbodyload %}
